package cn.tedu._05mvcboot02.exception;

import cn.tedu._05mvcboot02.common.resonse.JsonResult;
import cn.tedu._05mvcboot02.common.resonse.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    //处理异常
    @ExceptionHandler
    public JsonResult doHandlerIllegalArgumentException(IllegalArgumentException ex){
        String message = ex.getMessage();
        log.error("IllegalArgument:"+message);
        return new JsonResult(StatusCode.OPERATION_FAILED,message);
    }

    //父类处理异常
    @ExceptionHandler
    public JsonResult doHandlerRuntimeException(RuntimeException ex){
        String message =ex.getMessage();
        log.error("Runtime:"+message);
        return  new JsonResult(StatusCode.OPERATION_FAILED,message);
    }

@ExceptionHandler
    public  JsonResult doHandlerMethodArgumentNotValid(MethodArgumentNotValidException ex){
        return new JsonResult(StatusCode.VALIDATED_ERROR,ex.getMessage());
}
    //能够处理所有异常的方法
//    @ExceptionHandler
//    public JsonResult doHandleThrowableException(Throwable ex){
//        String message =ex.getMessage();
//        log.error("Throwable:"+message);
//        return new JsonResult(StatusCode.THROWABLE_ERROR,message);
//    }
}
